/* Ugame jquery function */
$.fn.flowUp = function(options){
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];
    
    $(this).animate({
        "position":"absolute",
        "top": "-=" + $(this).height() * step + "px"
    },
    duration,
    callback
    );
}
$.fn.flowDown = function(options){
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];
    
    $(this).animate({
        "position":"absolute",
        "top": "+=" + $(this).height() * step + "px"
    },
    duration,
    callback
    );
}
/* ACTION PANEL */
$.fn.processScrollEventOnActionPanel = function(){
    if(!$(this).is(':hidden')){
        $(this).hide();
        $(this).flowUp({
            step:"1",
            duration:"normal"                    
        });           
    }
}
$.fn.addActionClickHandlerOnActionPanel = function(options){
    var panel = options["panel"];
    var panels = options["disablePanels"];
    //init position for panel
    $(panel).flowUp({
        step:"1",
        duration:"normal"
    });
    //register Scroll handler for window
    $(window).scroll(function(){
        $(panel).processScrollEventOnActionPanel();
    }).resize(function(){
        $(panel).processScrollEventOnActionPanel();
    });
    //register Click handler for button
    $(this).click(function(){
        //Hide all disable Panels
        jQuery.each(panels, function(i, panel) {
            if(!$(panel).is(':hidden')){
                $(panel).hide();
                $(panel).flowUp({
                    step:"1",
                    duration:"normal"                    
                });           
            }
        });
        //Show or hide panel
        if(!$(panel).is(':hidden')){
            $(panel).flowUp({
                step:"1",
                duration:"normal",
                callback: function(){
                    $(panel).hide();
                }
            });
           
        }
        else
        {
            $(panel).show();
            $(panel).flowDown({
                step:"1",
                duration:"normal"
            });            
        }
    });
}
$("#btn-login").addActionClickHandlerOnActionPanel({
    panel:"#login-panel",
    disablePanels: ["#language-panel", "#search-panel"]
});
$("#btn-search").addActionClickHandlerOnActionPanel({
    panel:"#search-panel",
    disablePanels: ["#language-panel", "#login-panel"]
});
$("#btn-language").addActionClickHandlerOnActionPanel({
    panel:"#language-panel",
    disablePanels: ["#login-panel", "#search-panel"]
});
/* Categories Panel*/
function drawCategoryDock(options) {
    var mainNav = options["mainNav"];
    var controlBtn = options["controlBtn"];
    var subNav = options["subNav"];
    var config = options["config"];
    
    $(subNav).hide();
    $(controlBtn).parent().removeClass("active");
   
    var bottom = $("body").height() - ($(window).height() + $(window).scrollTop());
    
    if (bottom > config.showBottom) {
        $(mainNav).css({
            position: "fixed", 
            bottom: "0px"
        });
    } else {
        $(mainNav).css({
            position: "absolute", 
            bottom: config.currentBottom + "px"
        });
    }
}
$.fn.controlSubCategoryNav = function controlSubCategoryNav(options) {
    $(this).click(function(){
        var mainNav = options["mainNav"];
        var subNav = options["subNav"];
        var config = options["config"];
    
        var categoryItemWidth = $(this).width() - 1;

        var bottom = $("body").height() - ($(window).height() + $(window).scrollTop());
        if (bottom > 247) {
            $(subNav).css({
                width:categoryItemWidth + "px",
                position: "fixed",
                bottom: $(mainNav).height()+"px"
            });
        } else {
            $(subNav).css({
                width:categoryItemWidth + "px",
                position: "absolute",
                bottom: $(mainNav).height()+ config.currentBottom+"px"
            });
        }
        if ($(subNav).is(':hidden')) {
            $(this).parent().addClass("active");
        } else {
            $(this).parent().removeClass("active");
        }
        $(subNav).slideToggle("normal");
    });
}